@include('layouts.header')
<div class="layui-layout layui-layout-admin">
    @include("layouts.header-menu")
    @include("layouts.menu")

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div class="layui-form" style="padding: 15px;">
            <input type="hidden" name="info_id" id="info_id" value="{{ data_get($info, 'id') }}">
            <input type="hidden" name="news_type" id="news_type" value="2">
            <div class="layui-form-item">
                <label class="layui-form-label">标题: <span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" name="title" placeholder="标题名称" value="{{ data_get($info, 'title') }}" lay-verify="required" class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类: <span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <select name="class_id">
                        <option value="">请选择分类</option>
                        @foreach($category['list'] as $key=>$val)
                        <option value="{{ $val['id'] }}" @if ($val['id'] == data_get($info, 'class_id')) selected @endif>{{ $val['class_name'] }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容: <span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <textarea name="content" id="content" style="height: 607px;" placeholder="请输入系统公告内容" class="layui-textarea">{{ data_get($info, "content", "") }}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图片: <span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <div id="zhengshu_up_id">
                        @foreach(data_get($info, "images", []) as $key => $val)
                            <img src="{{ $val }}" width="100" height="100" />
                        @endforeach
                    </div>
                    <input type="hidden" name="images" id="zhengshu" value="{{ json_encode(data_get($info, "images", "")) }}" />
                    <button type="button" class="layui-btn" id="zhengshuBtn">上传图片（可多张）</button>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="*">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
@include("layouts.footer")

<script type="text/javascript">
    layui.extend({
        tinymce: '/static/js/tinymce/tinymce'
    });
    layui.use(['form', 'element','layer', 'jquery', 'util', 'tinymce', 'upload'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form,
            upload = layui.upload;
        var t = layui.tinymce;

        var edit = t.render({
            elem: "#content",
            automatic_uploads: false,
            images_upload_url: '/upload/images',
        },(opt, edit)=>{
            // 加载完成后回调 opt 是传入的所有参数
            // edit是当前编辑器实例，等同于t.get返回值
        });

        form.on('submit(*)', function(forms){
            layer.load('loading', {time: 10000});
            forms.field['_token'] = '{{ csrf_token() }}';
            forms.field.content = edit.getContent();
            $.post('/business/create', forms.field, function (data){
                layer.closeAll('loading');
                if (data.code == 0){
                    layer.msg('保存成功', {icon:1});
                    setTimeout(function (){window.parent.location.reload()}, 2000);
                }else{
                    layer.msg(data.msg, {icon:2});
                }
            },'json');
            return false;
        });


        // 荣誉证书
        var zhengshu_up = [];
        upload.render({
            elem: "#zhengshuBtn",
            url: '/common/uploads/images',
            filed: 'file',
            accept: 'images',
            data: {
                '_token': '{{ csrf_token() }}'
            },
            acceptMime: 'image/jpeg, image/jpg, image/png, image/gif',
            exts: 'jpg|png|jpeg|gif',
            auto: true,
            size: 1024 * 1024 * 2,
            multiple: true,
            number: 10,
            done: function(res, index, upload) {
                if (res.code != 0) {
                    layer.msg(res.msg, {icon: 2});
                }else{
                    zhengshu_up.push(res.body.src);
                }
            },
            allDone: function(){
                $("#zhengshu_up_id").html("");
                $("#zhengshu").val(JSON.stringify(zhengshu_up));
                $.each(zhengshu_up, function(index, value){
                    var _html = '<img src="'+value+'" width=100 height=100 />';
                    $("#zhengshu_up_id").append(_html);
                });
            }
        });
    });
</script>